package bb.lanxing.model.json;

import com.github.mikephil.charting.utils.Utils;
import bb.lanxing.manager.RemoteSharedPreference;
import bb.lanxing.model.database.IWorkout;
import bb.lanxing.util.JsonUtil;
import bb.lanxing.util.Log;
import bb.lanxing.util.SectionParseUtil;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class PowerSection {
    private static final String TAG = "PowerSection";
    public static final int ITEM_COUNT = 8;
    private static final String KEY_COUNTS = "counts";
    private static final String KEY_SECTIONS = "sections";
    private int[] counts;
    private int[] percents;
    private int[] sections;

    public void init(IWorkout workout) {
        int[] iArr;
        int[] iArr2;
        int powerCountByWorkout = (int) workout.getPowerCountByWorkout();
        Log.v(TAG, "init 1, countTotal = " + powerCountByWorkout);
        if (powerCountByWorkout <= 0) {
            return;
        }
        double powerFTP = workout.getPowerFTP();
        if (workout.getPowerFTP() == Utils.DOUBLE_EPSILON) {
            powerFTP = RemoteSharedPreference.getInstance().getPowerMax();
        }
        int[] parse = SectionParseUtil.parse(savePowerSection((float) powerFTP), 7);
        sections = parse;
        if (parse == null || parse.length <= 0) {
            return;
        }
        for (int i : parse) {
            Log.v(TAG, "init 1, section = " + i);
        }
        counts = new int[sections.length];
        int i2 = 0;
        while (true) {
            iArr = counts;
            if (i2 >= iArr.length) {
                break;
            }
            if (i2 < iArr.length - 1) {
                int[] iArr3 = sections;
                iArr[i2] = (int) workout.getCountInPowerSection(iArr3[i2], iArr3[i2 + 1]);
            } else {
                iArr[i2] = (int) workout.getCountInPowerSection(sections[i2], -1);
            }
            i2++;
        }
        for (int i3 : iArr) {
            Log.v(TAG, "init 1, count = " + i3);
        }
        percents = new int[counts.length];
        int i4 = 0;
        while (true) {
            iArr2 = percents;
            if (i4 >= iArr2.length) {
                break;
            }
            if (i4 == 0) {
                int[] iArr4 = counts;
                iArr2[i4] = ((iArr4[i4] + iArr4[iArr2.length - 1]) * 100) / powerCountByWorkout;
            } else {
                iArr2[i4] = (counts[i4] * 100) / powerCountByWorkout;
            }
            i4++;
        }
        for (int i5 : iArr2) {
            Log.v(TAG, "init 1, percent = " + i5);
        }
    }

    public void init(String jsonString) {
//        int[] iArr;
        int[] iArr2;
        try {
            JSONObject jSONObject = new JSONObject(jsonString);
            JSONArray arrayValue = JsonUtil.getArrayValue(KEY_SECTIONS, jSONObject);
            JSONArray arrayValue2 = JsonUtil.getArrayValue(KEY_COUNTS, jSONObject);
            int[] parse = SectionParseUtil.parse(arrayValue, ITEM_COUNT);
            sections = parse;
            if (parse != null && parse.length > 0) {
                int[] parse2 = SectionParseUtil.parse(arrayValue2, ITEM_COUNT);
                counts = parse2;
                if (parse2 != null && parse2.length > 0) {
                    int i = 0;
                    int i2 = 0;
                    while (true) {
                        int[] iArr3 = counts;
                        if (i >= iArr3.length) {
                            break;
                        }
                        i2 += iArr3[i];
                        i++;
                    }
                    for (int i3 : sections) {
                        Log.v(TAG, "init 2, scetion = " + i3);
                    }
                    for (int i4 : counts) {
                        Log.v(TAG, "init 2, count = " + i4);
                    }
                    Log.v(TAG, "init 2, totalCount = " + i2);
                    int[] iArr4 = sections;
                    if (iArr4 != null && (iArr2 = counts) != null && iArr4.length > 0 && iArr2.length > 0 && iArr4.length == iArr2.length) {
                        percents = new int[iArr2.length - 1];
                        int i5 = 0;
                        while (true) {
                            int[] iArr5 = percents;
                            if (i5 >= iArr5.length) {
                                break;
                            }
                            if (i5 == 0) {
                                int[] iArr6 = counts;
                                iArr5[i5] = ((iArr6[i5] + iArr6[iArr5.length]) * 100) / i2;
                            } else {
                                iArr5[i5] = (counts[i5] * 100) / i2;
                            }
                            i5++;
                        }
                    }
                    for (int i6 : percents) {
                        Log.v(TAG, "init 2, percent = " + i6);
                    }
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public int[] getSections() {
        return sections;
    }

    public int[] getCounts() {
        return counts;
    }

    public int[] getPercents() {
        return percents;
    }

    public String toJsonString() {
        String str = null;
        if (sections != null && counts != null) {
            JSONArray jSONArray = new JSONArray();
            for (int i : sections) {
                jSONArray.put(i);
            }
            JSONArray jSONArray2 = new JSONArray();
            for (int i2 : counts) {
                jSONArray2.put(i2);
            }
            try {
                JSONObject jSONObject = new JSONObject();
                jSONObject.put(KEY_SECTIONS, jSONArray);
                jSONObject.put(KEY_COUNTS, jSONArray2);
                str = jSONObject.toString();
            } catch (JSONException e) {
                e.printStackTrace();
            }
            Log.v(TAG, "toJsonString: " + str);
        }
        return str;
    }

    private String savePowerSection(float ftp) {
        int[] iArr = {0, 0, 0, 0, 0, 0, 0, 0};
        double[] dArr = {Utils.DOUBLE_EPSILON, 0.56d, 0.75d, 0.91d, 1.06d, 1.21d, 1.5d};
        for (int i = 0; i < ITEM_COUNT; i++) {
            if (i == 0) {
                iArr[i] = (int) (ftp * dArr[i]);
            } else {
                iArr[i] = (int) (ftp * dArr[i - 1]);
            }
        }
        String str = "";
        for (int i2 = 1; i2 < ITEM_COUNT; i2++) {
            str = str + iArr[i2];
            if (i2 < 7) {
                str = str + ";";
            }
        }
        return str;
    }
}
